class ForumsController < ResourceController

  def index
    @forums = Forum.find_top
  end
  
  def show
    @pager = Pager.new(topics_per_page, @forum.topics.count, params[:page])
    @topics = @forum.topics.find_ordered(@pager.find_options)
    respond_to do |wants|
      wants.html  # REFACTORME!  Surely some of this can be simplified
      wants.rss { render_rss_feed_for @topics, :feed => {:title => @forum.name, :link => forum_url(@forum)} }
      wants.atom { render_atom_feed_for @topics, :feed => {:title => @forum.name, :link => forum_url(@forum)} }
      wants.xml { render :xml => @forum.to_xml(:include => :topics) }
    end
  end
  
  # render new
  
  def create
    if @forum.save
      flash[:notice] = "Successfully created forum"
      redirect_to forums_path
    else
      render :action => 'new'
    end
  end
  
  # render edit
  
  def update
    if @forum.save
      flash[:notice] = "Successfully updated forum"
      redirect_to forums_path
    else
      render :action => 'edit'
    end
  end
  
  def destroy
    @forum.destroy
    flash[:notice] = "Successfully destroyed forum"
    redirect_to forums_path
  end
  
  def sortable
    @forums = Forum.find_top
  end
  
  def sort
    forum_key = params.keys.detect { |k| k =~ /^forums_\d+$/ }
    params[forum_key].each_with_index do |id, position|
      Forum.update(id, :position => position+1)
    end
    render :nothing => true
  end
  
  protected
  
    def permission_actions
      case params[:action].to_sym
        when :sortable, :sort then [:view, :sort]
        else super
      end
    end
  
end
